package day0718;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;

public class test {
    public static void main(String[] args) {
        JPanel jp = new JPanel();
        jp.setLayout(null);
        JLabel jl = new JLabel(new ImageIcon("images/背景.jpg"));
        jl.setLocation(0, 0);
        jl.setSize(480, 800);
        jp.add(jl);
        ArrayList<JButton> btns = creatBtns();//144
        Collections.shuffle(btns);
        putBtns(btns, jp, 20, 40, 7, 7, 0);//36,35,24
        putBtns(btns, jp, 50, 80, 6, 6, 7 * 7);//36,35,24  49+36
        putBtns(btns, jp, 15, 100, 5, 7, 85);//36,35,24
        putBtns(btns, jp, 45, 140, 4, 6, 120);//36,35,24
        checkCover(btns);
        ArrayList<JButton> throughBottomBtns =new ArrayList<>();
        addClickAction( btns,throughBottomBtns);
        /*for (int i = 0; i < btns.size(); i++) {
            JButton btn=btns.get(i);
            if(checkCover(btns, btn,start+i)){
                btn.setEnabled(false);
            }
        }*/

        JFrame jf = new JFrame("羊了个羊");
        jf.setSize(480, 800);
        jf.add(jp);
        jf.setVisible(true);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private static void addClickAction(ArrayList<JButton> btns,ArrayList<JButton> bottombtn) {
        for (int i = 0; i < btns.size(); i++) {
            JButton btn=btns.get(i);
            btn.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    btns.remove(btn);
                    bottombtn.add(btn);
                    btn.setLocation((bottombtn.size()-1)*60+27,640);
                }
            });
        }
    }



    //   144             前49个
    static void putBtns(ArrayList<JButton> btns, JPanel jp, int x, int y, int row, int col, int start) {//20,40
        for (int i = 0; i < col * row; i++) {
            //int index=(int)(Math.random()*i);
            JButton btn = btns.get(start + i);////////////////////////////
            btn.setSize(59, 66);
            btn.setLocation(i % col * 59 + x, i / col * 66 + y);
            jp.add(btn, 0);
        }
    }

    static void checkCover(ArrayList<JButton> btns) {
        for (int i = 0; i < btns.size() - 24; i++) {
            JButton button = btns.get(i);
            int x = button.getX() - 59;
            int x1 = button.getX() + 59;
            int y = button.getY() - 66;
            int y1 = button.getY() + 66;
            for (int j = i+1; j < btns.size(); j++) {
                JButton topb= btns.get(j);
                int x2=topb.getX();
                int y2=topb.getY();
                if (x<x2 & x2<x1 & y<y2 & y2<y1){
                    button.setEnabled(false);
                }
            }
        }
    }

    static ArrayList<JButton> creatBtns() {
        String[] a = new String[]{"兔子", "刷子", "剪刀", "南瓜", "奶瓶", "帽子", "干草", "手套", "月饼", "树", "树桩", "栗子",
                "毛线", "水桶", "火堆", "白菜", "耙子", "苹果", "草", "萝卜", "蜜蜂", "酒坛", "铃铛", "面具", "饭团"};
        ArrayList<JButton> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 25; j++) {
                JButton btn = new JButton(new ImageIcon("images/" + a[j] + ".png"));
                btn.setDisabledIcon(new ImageIcon("images/" + a[j] + "2.png"));
                list.add(btn);
            }
        }
        return list;
    }


}
